/**
 * \file combat.cpp
 * \brief Gestion des combats
 * \author ARTUR Vincent
 * \author LAPERDRIX Pierre
 * \version 1.0
 */

#include "combat.h"


using namespace std;


Combat::Combat(){
#ifndef INITRAND
srand(time(NULL));
#define INITRAND
#endif
}

Combat::~Combat(){}

int* Combat::attaque(int* tableAtt,int* tableDef, int nbCoups){
	int attA = tableAtt[ATTAQUE];
	int defA = tableAtt[DEFENSE];
	int pvA = tableAtt[PV];
	int pvcA = tableAtt[PVCREATION];
	int attD = tableDef[ATTAQUE];
	int defD = tableDef[DEFENSE];
	int pvD = tableDef[PV];
	int pvcD = tableDef[PVCREATION];

	//Probabilite de perte de PV de l'attaquant
	double rap = 0.0;
	bool uniteVaincue = false;
	while(nbCoups>0 && !uniteVaincue){
		rap = rapport(attA,pvA,pvcA,defD,pvD,pvcD);
		int alea =  rand() % 100;
		if(alea<rap*100){
			pvA--;
		}else{
			pvD--;
		}
		uniteVaincue |= (pvA<=0)||(pvD<=0);
		nbCoups--;
	}
	/* C'est pas beau */
	int* res = (int *)malloc(3*sizeof(int));
	res[0] = nbCoups;
	res[1] = pvA;
	res[2] = pvD;

	return res;

}

double Combat::rapport(int attA, int pvA,int pvcA, int defD, int pvD,int pvcD){
	int att = ((double)pvA/(double)pvcA)*((double)attA);
	int def = ((double)pvD/(double)pvcD)*((double)defD);
	if(att<def){
		return 0.5+0.5*(1-att/def);
	} else {
		return 0.5-0.5*def/att;
	}
}

Combat* Combat_new(){return new Combat();}
void Combat_delete(Combat* combat){delete combat;}
int* Combat_attaque(Combat* combat,int* tableAtt,int* tableDef, int nbCoups){return combat->attaque(tableAtt,tableDef,nbCoups);}